package cn.rcth.base.serivce.impl;

import cn.rcth.base.domain.Product;
import cn.rcth.base.domain.ProductImg;
import cn.rcth.base.domain.ProductTag;
import cn.rcth.base.domain.SpecialUrl;
import cn.rcth.base.mapper.ProductImgMapper;
import cn.rcth.base.mapper.ProductMapper;
import cn.rcth.base.mapper.ProductTagMapper;
import cn.rcth.base.mapper.SpecialUrlMapper;
import cn.rcth.base.serivce.IProductSerivce;
import cn.rcth.base.util.PageResult;
import cn.rcth.business.VO.ProductVO;
import cn.rcth.business.query.ProductQueryObject;
import cn.rcth.business.util.SevenUploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ProductSerivceImpl implements IProductSerivce {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private SevenUploadUtil sevenUploadUtil;

    @Autowired
    private ProductTagMapper productTagMapper;

    @Autowired
    private ProductImgMapper productImgMapper;

    @Autowired
    private SpecialUrlMapper specialUrlMapper;

    /**
     * 查询商品所有
     * @return
     */
    @Override
    public List<Product> getProductAll() {
        return productMapper.selectAll();
    }

    /**
     * 查询商品详情
     * @param productId 商品id
     * @return  商品详情
     */
    @Override
    public Map getProduct(Long productId) {

        Map<String, Object> map = new HashMap<>();
        Product product = productMapper.selectByPrimaryKey(productId);
        if (product == null){
            map.put("code","库存不足");
            return map;
        }
        if (product.getSpecial() == 0){
            ProductTag productTag = productTagMapper.selectProductTag(product.getId());
            List<ProductImg> productImgs =productImgMapper.selectProductImgById(product.getId());
            map.put("product",product);
            map.put("productTag",productTag);
            map.put("productImgs",productImgs);
            return map ;
        }else {
            SpecialUrl specialUrl = specialUrlMapper.selectByProductId(product.getId());
            map.put("productName",product.getName());
            map.put("productImg",product.getMaxImg());
            map.put("url",specialUrl.getUrl());
            return map;
        }
    }

    @Override
    public Product selectProduct(Long productId) {
        return productMapper.selectByPrimaryKey(productId);
    }

    @Override
    public void updateProduct(Product product) {
        productMapper.updateByPrimaryKey(product);
    }


}
